<template>
    <div class="auditContainer">
        <div class="lfCon1">
            <el-checkbox v-model="itemData.isCommonAudit" @change="changecA" class="lfCon1-item" :disabled="props.isRead">通用</el-checkbox>
            <dept-common 
                :isRead="props.isRead" 
                :isCheck="itemData.isCommonAudit" 
                :itemAudit="itemData.commonAudit" 
                :options1="options1" 
                :options2="options2" 
                :options3="options3" 
                @changeAudit="changeAudit1" 
                @saveMult="saveMult1"
                @changeSpeAuditType="changeSpeAuditType1"
                @changeDirectUserCode="changeDirectUserCode1"
                @changeFormColumn="changeFormColumn1"
                @changeClassName="changeClassName1"
                @changeIsAllow="changeIsAllow1"
            />
        </div>
        <div class="lfCon1">
            <div>
                <el-checkbox v-model="itemData.isDirectAudit" class="lfCon1-item" :disabled="props.isRead">指定部门</el-checkbox>
                <ul style="margin-left: 22px;" v-if="itemData.isDirectAudit">
                    <li v-for="(item,index) in deptList" :key="index">
                        <el-checkbox v-model="item.isCheck" class="lfCon1-item" :disabled="props.isRead">{{ item.name }}</el-checkbox>
                        <dept-common 
                        :isRead="props.isRead" 
                        :isCheck="item.isCheck" 
                        :itemAudit="item.itemAudit" 
                        :options1="options1" 
                        :options2="options2" 
                        :options3="options3" 
                        :index="index"
                        @changeAudit="changeAudit2" 
                        @saveMult="saveMult2"
                        @changeSpeAuditType="changeSpeAuditType2"
                        @changeDirectUserCode="changeDirectUserCode2"
                        @changeFormColumn="changeFormColumn2"
                        @changeClassName="changeClassName2"
                        @changeIsAllow="changeIsAllow2"
                    />
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
// 引入api
import flowApi from '@/api/flow';
import type { NodeDataType } from './index.vue';
import useDictStore from '@/store/modules/dict';
import DeptCommon from './deptCommon.vue';

const props = defineProps({
    isRead: {
        type: Boolean,
        default: false,
    }
})
let dictStore = useDictStore();
const nodeData = inject('nodeData', ref<NodeDataType>({
    id: null,
    type: null,
    typeName: null,
    code: null,
    name: null,
    position: { x: 0, y: 0 },
}));
const itemData = ref<any>({});
// 审核类型
const changeSpeAuditType1 = (msg: any)=>{
    itemData.value.commonAudit.dictSpeAuditType = msg.val;
}
const changeSpeAuditType2 = (msg: any)=>{
    deptList.value[msg.index].itemAudit.dictSpeAuditType = msg.val;
}
const saveMult1 = (msg:any)=>{
    itemData.value.commonAudit.candidate = msg.val.items;    
}
const saveMult2 = (msg:any)=>{
    deptList.value[msg.index].itemAudit.candidate = msg.val.items;    
}
const changeDirectUserCode1 = (msg:any)=>{
    itemData.value.commonAudit.directUserCode = msg.val;
}
const changeDirectUserCode2 = (msg:any)=>{
    deptList.value[msg.index].itemAudit.directUserCode = msg.val;
}
const changeFormColumn1 = (msg:any)=>{
    itemData.value.commonAudit.formColumn = msg.val;
}
const changeFormColumn2 = (msg:any)=>{
    deptList.value[msg.index].itemAudit.formColumn = msg.val;
}
const changeClassName1 = (msg:any)=>{
    itemData.value.commonAudit.className = msg.val;
}
const changeClassName2 = (msg:any)=>{
    deptList.value[msg.index].itemAudit.className = msg.val;
}
const changeIsAllow1 = (msg:any)=>{
    itemData.value.commonAudit.isAllowAddCountersign = msg.val;
}
const changeIsAllow2 = (msg:any)=>{
    deptList.value[msg.index].itemAudit.isAllowAddCountersign = msg.val;
    console.log('当前审核人2:',deptList.value[msg.index].itemAudit)
}
// 审核人
const changeAudit1 = (msg:any)=>{   
    // console.log('审核人变更1:',msg); 
    itemData.value.commonAudit.dictSpeAuditor = msg.val;
    itemData.value.commonAudit.candidate = [];
    itemData.value.commonAudit.formColumn = '';
    itemData.value.commonAudit.directUserCode = [];
    itemData.value.commonAudit.className = '';
    // console.log('当前审核人1:',itemData.value)
}
const changeAudit2 = (msg: any)=>{    
    // console.log('审核人变更2:',msg,deptList.value[msg.index],deptList.value[msg.index].itemAudit);
    deptList.value[msg.index].itemAudit.dictSpeAuditor = msg.val;
    deptList.value[msg.index].itemAudit.candidate = [];
    deptList.value[msg.index].itemAudit.formColumn = '';
    deptList.value[msg.index].itemAudit.directUserCode = [];
    deptList.value[msg.index].itemAudit.className = '';
    // console.log('当前审核人2:',deptList.value[msg.index].itemAudit.dictSpeAuditType)
}

const options1 = ref<any[]>([]);
const options2 = ref<any[]>([]);
const options3 = ref<any[]>([]);
const deptList = ref<any[]>([]);

const initializeData = async () => {
    try {
        itemData.value = nodeData.value.businessData;
        options1.value = dictStore.dicts.SPE_AUDIT_TYPE.list;
        options2.value = dictStore.dicts.SPE_AUDITOR.list;
        options3.value = await flowApi.getDynamicsUserList() as any;

        const deptListOld = dictStore.dicts.DEPT_CFG_CATEGORY.list;
        if (deptListOld && deptListOld.length > 0) {
            deptList.value = deptListOld
                .filter((item: any) => parseInt(item.code) > 0)
                .map((item: any) => ({
                    name: item.name,
                    code: item.code,
                    isCheck: false,
                    itemAudit: {
                        dictSpeAuditType: '1',
                        dictSpeAuditor: '3',
                        candidate: [],
                        directUserCode: null,
                        formColumn: '',
                        className: '',
                        isAllowAddCountersign: 1
                    }
                }));

            if (itemData.value.directAudits && itemData.value.directAudits.length > 0) {
                itemData.value.directAudits.forEach((item: any) => {
                    const matchedDept = deptList.value.find(dept => dept.code === item.dictDeptCfgCategory);
                    if (matchedDept) {
                        matchedDept.isCheck = true;
                        matchedDept.itemAudit = {
                            ...matchedDept.itemAudit,
                            ...item
                        };
                    }
                });
            }
        }
    } catch (error) {
        console.error('Error initializing data:', error);
    }
}
const changecA = () => {
    // console.log('数据businessData:',itemData.value);
}
onMounted(async () => {
    await initializeData()
})
const isInfoPass = ref(true);
const allNotChecked = ref(true);
const doSave = () => {
    itemData.value.directAudits = deptList.value
        .filter(item => item.isCheck)
        .map(item => ({
            dictDeptCfgCategory: item.code,
            dictSpeAuditType: item.itemAudit.dictSpeAuditType,
            dictSpeAuditor: item.itemAudit.dictSpeAuditor,
            candidate: toMyStr(item.itemAudit.candidate),
            directUserCode: item.itemAudit.directUserCode,
            formColumn: item.itemAudit.formColumn,
            className: item.itemAudit.className,
            isAllowAddCountersign: item.itemAudit.isAllowAddCountersign
        }));
}
const doCheck = () => {
    isInfoPass.value = true;
    if(itemData.value.isCommonAudit){
        if((!itemData.value.commonAudit.candidate || (itemData.value.commonAudit.candidate && itemData.value.commonAudit.candidate.length==0)) && 
        (!itemData.value.commonAudit.directUserCode || (itemData.value.commonAudit.directUserCode && itemData.value.commonAudit.directUserCode.length==0)) && 
            !itemData.value.commonAudit.formColumn && 
            !itemData.value.commonAudit.className 
        ){
            isInfoPass.value = false;
            
            ElMessage.error('审核人-通用存在未完善的信息！');
            return
        }
    }
    if(itemData.value.isDirectAudit){
        allNotChecked.value = true; // 假设所有部门都没有被选中
        for (let i = 0; i < deptList.value.length; i++) {
            if (deptList.value[i].isCheck) {
                allNotChecked.value = false; // 如果有一个部门被选中，则设置为false
                if ((!deptList.value[i].itemAudit.candidate||(deptList.value[i].itemAudit.candidate&&deptList.value[i].itemAudit.candidate.length==0)) && 
                    (!deptList.value[i].itemAudit.directUserCode||(deptList.value[i].itemAudit.directUserCode&&deptList.value[i].itemAudit.directUserCode.length==0)) && 
                    !deptList.value[i].itemAudit.formColumn && 
                    !deptList.value[i].itemAudit.className 
                ) {
                    isInfoPass.value = false;
                    break;
                }
            }
        }
        if (allNotChecked.value || !isInfoPass.value) {
            ElMessage.error('审核人-指定部门存在未完善的信息！');
            return
        }
    }
}
const toMyStr = (val: any)=> {
    if (!val ||( Array.isArray(val) && val.length === 0)) {
        return null;
    }else{
        return val;
    }
}
defineExpose({
    isInfoPass, allNotChecked, doCheck, doSave, itemData
});
</script>
<style scoped lang="scss">
.auditContainer {
    margin: 10px 10px 0;
}
</style>
